package cn.biplam.front.component.view.systemManage.sysprivi.role.manage.privi;

import cn.biplam.common.ajxcode.SystemCode.RoleManageCode;
import cn.biplam.common.bean.systemManage.privi.RoleManageBean;
import cn.biplam.common.opcode.SysManageCode.RolePriviOptCode;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.view.systemManage.sysprivi.privilege.navi.PriviNavi;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.template.control.PromptContent;

/**
 *  添加角色权限
 * @author XiaoRuxing Dec 9, 20097:43:19 PM
 * @version 1.0
 */
public class AddRolePrivi extends ComponentBase{

	private static final String cId = RoleManageCode.Role_Privi_Add;
	
	public AddRolePrivi(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
	}

	/**
	 * @return 获取权限选择界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow priviSelect() throws ErrorMessage{
		//获取权限导航树
		HtmlWindow tree = getPriviTree();
		//设置弹出窗口确定提交命令
		String script = AjxScript.getSpNoRefresh(SysName.PromoptContentID,
				RoleManageCode.Role_Privi_Add_Submit,
				recordId);
		
		PromptContent prompt = new PromptContent(tree);
		prompt.setSubmitScript(script);
		HtmlWindow win = prompt.get();
		return win;
	}
	
	/**
	 * @return 添加角色权限提交
	 * @throws ErrorMessage 
	 */
	public HtmlWindow submit() throws ErrorMessage{
		RoleManageBean manage = RoleManageBean.getInstance(mUser.getUserID());
		String roleId = recordId;
		String priviId = params.getByKey(TreeParam.NODE_ID);
		
		if(!(TOOLS.StringIsEmpty(roleId) || TOOLS.StringIsEmpty(priviId))){
			//添加角色权限
			manage.addRolePrivi(roleId, priviId);
		}
		
		//刷新角色权限信息列表
		HtmlWindow win = new HtmlDiv();
		win.bind(AjxScript.getA(RolePriviMain.cId, RoleManageCode.Role_Info));
		return win;
	}
	
	/**
	 * @return 权限导航树
	 */
	private HtmlWindow getPriviTree() throws ErrorMessage{
		PriviNavi navi = new PriviNavi();
		//设置节点过滤条件为当前角色具有的权限不显示
		navi.setFilterOperator(RolePriviOptCode.Query_Role_Privi_Imme,recordId);
		HtmlWindow priviTree =  navi.get(mUser, sHomeUrl, cId);
		priviTree.setStyleHeight(200);//设置树高度
		priviTree.setStyleWidth(240);//设置树高度
		return priviTree;
	}
}
